#include <STC15F2K60S2.H>
#include <intrins.H>

sbit TX=P1^0;
sbit RX=P1^1;

void Delay12us()		//@12.000MHz
{
	unsigned char i;

	_nop_();
	_nop_();
	i = 33;
	while (--i);
}


void Ultra_Init()
{
	TX=1;
	Delay12us();
	TX=0;
	Delay12us();
}

unsigned char Ultra_Wave()
{
	unsigned int Temp;
	TMOD&=0x0f;
	TH1=TL1=0;
	Ultra_Init();
	TR1=1;
	while(RX==1 && TF1==0);
	TR1=0;
	if(TF1==0)
	{
		Temp=TH1<<8|TL1;
		return Temp*0.017;
	}
	else
	{
		TF1=0;
		return 0;
	}
}